package fun.tan90.easy.binlog.worker.handler;

import com.github.benmanes.caffeine.cache.LoadingCache;
import fun.tan90.easy.binlog.base.constants.EasyBinlogConstants;
import fun.tan90.easy.binlog.base.strategy.AbstractExecuteStrategy;
import fun.tan90.easy.binlog.plugin.common.model.BinlogOutputRule;
import fun.tan90.easy.binlog.worker.cache.BinaryLogClientKey;
import fun.tan90.easy.binlog.worker.cache.DataSourceKey;
import lombok.extern.slf4j.Slf4j;
import org.noear.socketd.transport.core.Entity;
import org.noear.socketd.transport.core.Message;
import org.noear.socketd.transport.core.entity.StringEntity;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * description TODO
 * company 铁人科技
 *
 * @author 陈浩杰
 * @date 2023/12/15 19:42
 */
@Slf4j
@Component
public class TurnOffBinlogHandler implements AbstractExecuteStrategy<Message, Entity> {

    @Resource
    LoadingCache<String, Map<String, List<BinlogOutputRule>>> outputRuleCache;

    @Override
    public String mark() {
        return EasyBinlogConstants.TURN_OFF_BINLOG;
    }

    @Override
    public Entity executeResp(Message message) {
        String masterCode = message.dataAsString();
        BinaryLogClientKey.removeBinaryLogClient(masterCode);
        DataSourceKey.removeDatasource(masterCode);
        outputRuleCache.invalidate(masterCode);
        return new StringEntity("1");
    }
}
